package kliment.api.webMoney
{
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;

	import kliment.api.ICheckout;
	import kliment.data.ShopingCartItem;

	public class WebMoneyCheckout implements ICheckout
	{
		private const _URL:String = 'https://merchant.webmoney.ru/lmi/payment.asp';

		/**
		 * The merchant’s purse to which the customer has to pay. Format is a letter and twelve digits.
		 * Presently, Z, R, E and D purses are used in the service.
		 */
		public var merchantID:String = "";

		public var paymentNo:uint;
		public var paymentDesc:String;
		public var paymentDescBase64:String;
		public var simMode:int = -1;
		public var resultUrl:String;
		public var successUrl:String;
		public var successMethod:String;
		public var failUrl:String;
		public var failMethod:String;
		public var paymerPinnumberinside:String;
		public var wmnotePinnumberinside:String;
		public var paymerEmail:String;
		public var wmcheckNumberinside:String;
		public var wmcheckCodeinside:String;
		public var paymentCreditdays:String;

		private var _itemsCollection:Array = [];

		/**
		 * Constructor.
		 */
		public function WebMoneyCheckout()
		{
			super();
		}

		/**
		 * @inheritDoc
		 */
		public function get apiURL():String
		{
			return _URL;
		}

		/**
		 * @inheritDoc
		 */
		public function attachProducts(list:Array):Boolean
		{
			_itemsCollection = [];
			var length:uint = list.length;
			for (var i:uint = 0; i < length; i++)
			{
				_itemsCollection.push(convertCardItem(list[i], i + 1));
			}
			return false;
		}

		/**
		 * @inheritDoc
		 */
		public function getRequestVariables():URLVariables
		{
			//http://wiki.webmoney.ru/wiki/show/Web+Merchant+Interface
			var result:URLVariables = new URLVariables();
			result.LMI_PAYEE_PURSE = merchantID;

			if (paymentNo > 0)
				result.LMI_PAYMENT_NO = paymentNo;
			if (_validVar(paymentDesc))
				result.LMI_PAYMENT_DESC = paymentDesc;
			if (_validVar(paymentDesc))
				result.LMI_PAYMENT_DESC_BASE64 = paymentDescBase64;
			if (i > -1)
				result.LMI_SIM_MODE = simMode;
			if (_validVar(paymentDesc))
				result.LMI_RESULT_URL = resultUrl;
			if (_validVar(paymentDesc))
				result.LMI_SUCCESS_URL = successUrl;
			if (_validVar(paymentDesc))
				result.LMI_SUCCESS_METHOD = successMethod;
			if (_validVar(paymentDesc))
				result.LMI_FAIL_URL = failUrl;
			if (_validVar(paymentDesc))
				result.LMI_FAIL_METHOD = failMethod;
			if (_validVar(paymentDesc))
				result.lmi_paymer_pinnumberinside = paymerPinnumberinside;
			if (_validVar(paymentDesc))
				result.lmi_wmnote_pinnumberinside = wmnotePinnumberinside;
			if (_validVar(paymentDesc))
				result.lmi_paymer_email = paymerEmail;
			if (_validVar(paymentDesc))
				result.lmi_wmcheck_numberinside = wmcheckNumberinside;
			if (_validVar(paymentDesc))
				result.lmi_wmcheck_codeinside = wmcheckCodeinside;
			if (_validVar(paymentDesc))
				result.LMI_PAYMENT_CREDITDAYS = paymentCreditdays;
			return result;
		}

		/**
		 * @inheritDoc
		 */
		public function submit():Boolean
		{
			var length:uint = _itemsCollection.length;
			if (!length)
			{
				return false;
			}

			var urlRequest:URLRequest = new URLRequest(apiURL());
			var urlVars:URLVariables = getRequestVariables();

			var amount:Number = 0;
			var item:Object;
			for (var i:uint = 0; i < length; i++)
			{
				item = _itemsCollection[i];
				amount += Number(item);
			}

			urlVars.LMI_PAYMENT_AMOUNT = amount;
			urlRequest.data = urlVars;
			urlRequest.method = URLRequestMethod.POST;

			navigateToURL(urlRequest, "_blank");
			return true;
		}

		private function _validVar(value:String = null):Boolean
		{
			return  (value && value != '');
		}

		private function convertCardItem(value:ShopingCartItem, index:uint = 0):Object {
			var result:Number = value.price * value.quantity;
			return result;
		}
	}
}